<template>
  <div class="articaldetail">
    <div class="header">
      <div class="left">
        <van-icon name="arrow-left back" @click="$router.back()" />
        <span class="iconfont iconnew new"></span>
      </div>
      <span
        @click="handler_follow"
        :class="{ follow: articaldetail.has_follow }"
        >{{ articaldetail.has_follow ? "已关注" : "关注" }}</span
      >
    </div>
    <div class="detail">
      <div class="title">{{ articaldetail.title }}</div>
      <div class="desc">
        <!-- 这里用短路运算，逻辑且，如果都有值，就渲染后面一个，没有就都不渲染 -->
        <span>{{ articaldetail.user && articaldetail.user.nickname }}</span>
        &nbsp;&nbsp;
        <span>{{ articaldetail.create_date | getDate }}</span>
      </div>
      <div
        class="content"
        v-html="articaldetail.content"
        v-if="articaldetail.type == '1'"
      >
        文章的内容：
      </div>
      <div class="content" v-if="articaldetail.type == '2'">
        <video :src="articaldetail.content" controls></video>
      </div>
      <div class="opt" @click="handlerlike">
        <span class="like" :class="{ click_like: articaldetail.has_like }">
          <van-icon name="good-job-o" />{{ articaldetail.like_length }}
        </span>
        <span class="chat"> <van-icon name="chat" class="w" />微信 </span>
      </div>
    </div>
    <!-- 精彩跟帖 -->
    <div class="keeps">
      <h2>精彩跟帖</h2>
      <div class="item">
        <div class="head">
          <img src="../assets/logo.png" alt />
          <div>
            <p>火星网友</p>
            <span>2小时前</span>
          </div>
          <span>回复</span>
        </div>
        <div class="text">文章说得很有道理</div>
      </div>
      <div class="more">更多跟帖</div>
    </div>
    <myfooted :articaldetail="articaldetail"></myfooted>
  </div>
</template>

<script>
import myfooted from "@/components/myfooted";
import { getAritcalDetail, post_like, post_star } from "@/apis/article";
// 引入过滤器
import { getDate } from "@/filter/myfilter";
// 引用关注函数
import { user_follows, user_unfollow } from "@/apis/user";
export default {
  components: {
    myfooted,
  },
  data() {
    return {
      articaldetail: {},
    };
  },
  filters: {
    getDate,
  },
  methods: {
    // // 点击文章收藏
    // async handlerstar() {
    //   let res = await post_star(this.articaldetail.id);
    //   this.articaldetail.has_star = !this.articaldetail.has_star;
    //   console.log(this.articaldetail.has_star);
    // },
    // 点赞
    async handlerlike() {
      // console.log(1);
      let res = await post_like(this.articaldetail.id);
      this.articaldetail.has_like = !this.articaldetail.has_like;
      if (this.articaldetail.has_like) {
        this.articaldetail.like_length++;
      } else {
        this.articaldetail.like_length--;
      }
    },
    async handler_follow() {
      // 监听是否点击  先判段是否关注
      if (this.articaldetail.has_follow) {
        // 已经关注，现在是要取消关注
        let unfollow = await user_unfollow(this.articaldetail.user.id);
        // console.log(unfollow);
        if (unfollow.data.message == "取消关注成功") {
          this.$toast.success(unfollow.data.message);
          this.articaldetail.has_follow = false;
        }
      } else {
        // 没有关注，现在要关注
        let follow = await user_follows(this.articaldetail.user.id);
        // console.log(follow);
        if (follow.data.message == "关注成功") {
          this.$toast.success(follow.data.message);
          this.articaldetail.has_follow = true;
        }
      }
    },
  },

  async mounted() {
    // 1.根据id获取文章的详情，实现文章详情的动态渲染
    // 调用封装好的文章详情
    const res = await getAritcalDetail(this.$route.params.id);
    console.log(res.data.data);
    this.articaldetail = res.data.data; //2.把的到的值符给data的空对象里面 再渲染页面
  },
};
</script>

<style lang='less' scoped>
.articaldetail {
  padding-bottom: 40px;
}
.header {
  padding: 0px 10px;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ccc;
  > .left {
    height: 100%;
    vertical-align: middle;
    position: relative;
    .van-icon {
      position: absolute;
      top: 15px;
      left: 0;
    }
    > span {
      margin-left: 22px;
      font-size: 50px;
    }
  }
  > span {
    padding: 5px 15px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 15px;
    font-size: 13px;
  }
  .follow {
    padding: 5px 15px;
    background-color: transparent;
    color: #666;
    text-align: center;
    border-radius: 15px;
    font-size: 13px;
    border: 1px solid #ccc;
  }
}
.detail {
  padding: 15px;
  .title {
    font-size: 18px;
    font-weight: bold;
    padding: 10px 0;
  }
  .desc {
    line-height: 30px;
    color: #999;
    font-size: 13px;
  }
  .content {
    text-indent: 2em;
    line-height: 24px;
    font-size: 15px;
    padding-bottom: 30px;
    width: 100%;
    /deep/ img {
      width: 100%;
      display: block;
    }
    /deep/ video {
      width: 100%;
      display: block;
    }
  }
}
.opt {
  display: flex;
  justify-content: space-around;
  .like,
  .chat {
    height: 25px;
    padding: 0 15px;
    font-size: 14px;
    line-height: 25px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
  }
  .click_like {
    color: red;
  }
  .w {
    color: rgb(84, 163, 5);
  }
}
.keeps {
  border-top: 5px solid #ddd;
  padding: 0 15px;
  > h2 {
    line-height: 50px;
    text-align: center;
  }
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: 50/360 * 100vw;
        height: 50/360 * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
  .more {
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
    margin: 20px auto;
    font-size: 13px;
  }
}
// .comment {
//   position: fixed;
//   bottom: 0;
//   left: 0;
// }
</style>